package com.example.demo.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.sys.mapper.CoursemanagerMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.example.demo.controller.Result;
import com.example.demo.sys.mapper.StudentworkMapper;
import com.example.demo.sys.entity.Studentwork;

import org.springframework.web.bind.annotation.RestController;
import com.example.demo.common.tools.BaseController;

/**
 * <p>
    *  前端控制器 chrimer
    * </p>
 *
 * @author chrimer
 * @since 2020-07-01
 */
@RestController
@RequestMapping("/sys/studentwork")
public class StudentworkController extends BaseController {
    @Autowired
    private StudentworkMapper studentworkMapper;


    @RequestMapping(method= RequestMethod.POST,value="/page")
    public Result page(
        Integer pageNum,
        Integer pageSize,
        String userId,
        String courseId,
        String parentName) {
        Result result = new Result();
        Page<Studentwork> page = new Page<Studentwork>(pageNum, pageSize);
        Studentwork parms = new Studentwork();
        QueryWrapper<Studentwork> warpper = new QueryWrapper<Studentwork>(parms);

        if(parentName != null && !"".equals(parentName)){
            warpper.nested(i-> i.eq("courseId",courseId).eq("userId",userId).like("parentName",parentName));
        }
        else
            warpper.nested(i-> i.eq("courseId",courseId).eq("userId",userId));
        result.setData(studentworkMapper.selectPage(page, warpper));
        return result;
    }
    @RequestMapping(method= RequestMethod.POST,value="/save")
    public Result save(@RequestBody Studentwork entity) {
        Result result = new Result();
        Studentwork test = new Studentwork();
        test.setId(entity.getId());
        test.setUserId(entity.getUserId());
        Studentwork studentworkExit = studentworkMapper.selectOne(new QueryWrapper<Studentwork>(test));
        if (studentworkExit == null) {
            String parantName = studentworkMapper.SelectParentName(entity.getId());
            entity.setParentName(parantName);
            studentworkMapper.insert(entity);
            result.success(entity.getParentName()+"作业提交成功");
        } else {
            String parantName = studentworkMapper.SelectParentName(entity.getId());
            entity.setParentName(parantName);
            studentworkMapper.updateById(entity);
            result.success(entity.getParentName()+"作业成功替换");
        }
        return result;
    }

    //打分
    @RequestMapping(method= RequestMethod.POST,value="/savescore")
    public Result savescore(@RequestBody Studentwork entity) {
        Result result = new Result();
        studentworkMapper.updateById(entity);
        result.success("评分成功");
        return result;
    }


    //查看个人当前课程得分情况
    @RequestMapping(method= RequestMethod.POST,value="/selectscore")
    public Result select(String courseId,String userId) {
        //System.out.println(courseId+"用户id"+userId);
        Result result = new Result();
        List<Integer> scoreList = null;
        scoreList = studentworkMapper.SelectScore(userId,courseId);
        List<String> parentNameLit = null;
        parentNameLit = studentworkMapper.SelectparentName(userId,courseId);


        //更新学生当前总成绩
            int scoreSum = 0;
            List<Integer> score = studentworkMapper.SelectScore(userId,courseId);
            for(int j=0;j<score.size();j++)
            {
                scoreSum = scoreSum + score.get(j);
            }
            studentworkMapper.UpdateGrade(scoreSum,userId,courseId);


        Map<String, Object> map = new HashMap<>();
        map.put("scoreSum",scoreSum);
        map.put("scoreList", scoreList);
        map.put("parentNameLit", parentNameLit);
        result.setData(map);
        return result;
    }


    @RequestMapping(method= RequestMethod.DELETE,value="/delete")
    public Result delete( String ids) {
        Result result = new Result();
        List<String> deleteIds = new ArrayList<String>();
        for (String id : ids.split(",")) {
            deleteIds.add(id);
        }
    studentworkMapper.deleteBatchIds(deleteIds);
        return result;
    }

}
